package com.flow.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.flow.dao.IUserBasicDao;
import com.flow.model.PagerModel;
import com.flow.model.UserBasic;
import com.flow.service.IUserBasicService;
import com.flow.tools.StringTools;

/**
 * 会员基本信息服务层
 * 
 * @author wang
 * @2015-12-5 UserBasicService.java
 * @param <T>
 * 
 */
public class UserBasicService<T> implements IUserBasicService<T> {

	/**
	 * Dao层引用
	 */
	private IUserBasicDao<T> ubDao;


	/**
	 * 查询用户列表
	 */
	@Override
	public PagerModel findUserBasic(Map<String, Object> map) {

		StringBuffer hql = new StringBuffer("from UserBasic");
		List<Object> params = new ArrayList<Object>();
		PagerModel pager = new PagerModel();
		try {

			for (String key : map.keySet()) {
				if (null != map.get(key) && "" != map.get(key).toString()) {
					if (hql.toString().equals("from UserBasic")) {

						hql.append(" where " + key + " like ?");
						params.add("%" + map.get(key) + "%");
					} else {

						hql.append(" or " + key + " like ?");
						params.add("%" + map.get(key) + "%");
					}

				}
			}
			pager = ubDao.findUserBasicPagerModeled(pager, hql.toString(),
					params);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return pager;
	}

	/**
	 * 添加会员基本信息
	 */
	@Override
	public boolean insertUserBasic(UserBasic userBasic) {

		if (!StringTools.nullOrEmpty(userBasic.getUbId()))
			return false;

		if (!StringTools.nullOrEmpty(userBasic.getUbPwd()))
			return false;

		if (StringTools.nullOrEmpty(userBasic.getUbPhone())) {
			if (!validateTelphone(userBasic.getUbPhone()))
				return false;
		}

		if (StringTools.nullOrEmpty(userBasic.getUbEmail())) {
			if (!validateEmail(userBasic.getUbEmail()))
				return false;

		}
		
		if (!validateId(userBasic.getUbId()))
			return false;
		
		try {

			userBasic.setUbCreateTime(new Date());
			if (ubDao.insertUserBasic(userBasic))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;

	}

	/**
	 * 验证ID
	 */
	@Override
	public boolean validateId(String loginId) {

		if (!StringTools.nullOrEmpty(loginId))
			return false;

		try {

			String hql = "from UserBasic where ubId = ?";
			if (null == ubDao.findUserBasicByHql(hql, loginId))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;
	}

	/**
	 * 验证电话号码
	 */
	@Override
	public boolean validateTelphone(String telphone) {

		if (!StringTools.nullOrEmpty(telphone))
			return false;

		try {
			
			String hql = "from UserBasic where ubPhone = ?";
			if (null == ubDao.findUserBasicByHql(hql, telphone)) 
				return true;
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return false;
	}

	/**
	 * 验证邮箱
	 */
	@Override
	public boolean validateEmail(String email) {

		if (!StringTools.nullOrEmpty(email))
			return false;

		try {
			
			String hql = "from UserBasic where ubEmail = ?";
			if (null == ubDao.findUserBasicByHql(hql, email))
				return true;
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return false;
	}
	
	/**
	 * 根据ID查询
	 */
	@Override
	public UserBasic findUserBasicById(String ubId) {
		
		if (!StringTools.nullOrEmpty(ubId))
			return null;

		try {
			
			String hql = "from UserBasic where ubId = ?";
			return  this.ubDao.findUserBasicByHql(hql, ubId);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;
	}

	
	/**
	 * 更新用户
	 */
	@Override
	public boolean updateUserBasic(UserBasic userBasic) {

		if(null == userBasic)
			return false;

		if(!StringTools.nullOrEmpty(userBasic.getUbPwd()))
			return false;
		
		if(!StringTools.nullOrEmpty(userBasic.getUbId()))
			return false;
		
		if(null == userBasic.getUbStatus())
			return false;
		
		try {
			
			if(ubDao.updateUserBasic(userBasic))
				return true;
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return false;
		
	}

	/**
	 * 删除用户
	 */
	@Override
	public boolean deleteUserBasic(UserBasic userBasic) {
		
		if(null == userBasic)
			return false;
		
		if(!StringTools.nullOrEmpty(userBasic.getUbId()))
			return false;
		
		try {
			
			if(ubDao.deleteUserBasic(userBasic))
				return true;
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return false;
	}

	/**
	 * 登录方法
	 */
	@Override
	public UserBasic login(String loginName) {
		
		if(!StringTools.nullOrEmpty(loginName))
			return null;
		
		try {
			
			String hql = "from UserBasic where ubName = ? or ubPhone = ? or ubEmail = ?";
			List<Object> params = new ArrayList<Object>();
			params.add(loginName);
			params.add(loginName);
			params.add(loginName);
			return ubDao.findUserBasicByHql(hql, params);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return null;
	}
	
	/**
	 * 修改最后登录方法
	 */
	@Override
	public boolean updateLastTime(String ubId) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void setUbDao(IUserBasicDao<T> ubDao) {
		this.ubDao = ubDao;
	}


}
